package creational.singleton;

/**
 * @ClassName: HungrySingleton
 * @Description: 饿汉式
 * @Author: liu-hao
 * @Date: 2023-03-27 21:44
 * @Version: 1.0
 **/
public class HungrySingleton {

    // 此final可写可不写，策略问题，写上最好
    private static final HungrySingleton INSTANCE = new HungrySingleton();

    private HungrySingleton() {
        // 防止通过反射进行实例化（注意，此方式不严谨，反射可以修改单例对象引用）
        if (INSTANCE != null) {
            throw new IllegalStateException("单例对象已经初始化！");
        }
    }

    public static HungrySingleton getInstance() {
        return INSTANCE;
    }

}
